<?php

declare(strict_types=1);

namespace app\api\model;

use app\api\model\TeachStudentRelation as ModelTeachStudentRelation;
use app\api\model\Course as ModelCourse;
use app\api\model\Tool as ModelTool;
use app\api\model\Student as ModelStudent;
use app\api\model\Chapter as ModelChapter;

/**
 * 老师模型
 */
class Teacher extends Base
{
    protected $autoWriteTimestamp = 'datetime';

    /**
     * 获取单条记录
     */
    public static function get($id)
    {
        return self::where('status', self::STATUS_NORMAL)->find($id);
    }

    /**
     * 密码自动加密处理
     */
    public static function onBeforeInsert($user)
    {
        $user->password = password_hash(md5($user->password), PASSWORD_DEFAULT);
    }

    /**
     * 验证登录
     * @param 会员账户
     * @param 会员密码
     */
    public function checkLogin($account = '', $password = '')
    {
        $account  = trim($account);
        $password = trim($password);

        $map = [];
        $map[] = ['status', '=', 1];
        $map[] = ['account', '=', $account];

        $user = self::where($map)->field('id,account,password,avatar,amount,truename,chapter_count')->find();
        if (empty($user)) {
            $this->error = '用户不存在或被禁用！';
            return false;
        }
        $arrUser = $user->toArray();
        # 密码校验
        if (!password_verify($password, $arrUser['password'])) {
            $this->error = '登陆密码错误！';
            return false;
        }
        if ($user->chapter_count <= 0) {
            $chapter = ModelChapter::getTeachCreateChapter($user->id,0);
            if (empty($chapter->toArray())) {
                $arrUser['chapter_count'] = 0;
            } else {
                $user->chapter_count = count($chapter);
                $user->save();
                $arrUser['chapter_count'] = count($chapter);
            }
        }
        # 加密手机号
        ModelTool::encryptPhone($arrUser);
        # 获取老师所授课程
        $course = ModelCourse::getCourse($arrUser);
        $courseArr = $course->toArray();
        if (empty($courseArr)) {
            $arrUser['studentCount'] = 0;
            $arrUser['job'] = [];
        } else {
            $job = ModelTool::getKeyArray($courseArr, 'name');
            $arrUser['job'] = $job;
            $course_ids = ModelTool::getKeyArray($courseArr, 'id');
            # 获取老师名下学生人数
            $student = ModelStudent::getStudent($course_ids);
            if (empty($student->toArray())) {
                $arrUser['studentCount'] = 0;
            } else {
                $arrUser['studentCount'] = count($student);
            }
        }
        unset($arrUser['password']);
        return $arrUser;
    }

    /**
     * # 验证教师用户是否存在
     * @param srting|int $teacher_id
     * @return array|object $teacher
     */

    public static function verifyTeacher($teacher_id)
    {
        $teacher = Teacher::find($teacher_id);
        if (empty($teacher)) {
            return false;
        }
        return $teacher;
    }
}
